### 准备工作
import requests,json
import pandas as pd
key = "0183e3acc9bdff621d98d15d15a37181"
------------------------------------------------------------------
### 地理编码
东山中学_address = "广东省梅州市梅县东山中学"                 ### address 是需要获取坐标的结构化地址
url = "https://restapi.amap.com/v3/geocode/geo?parameters"
params = {                                                    ### params是参数的意思 使用字典的结构
    "key":key,
    "address":东山中学_address
}
response = requests.get(url,params=params)
response
### 响应结果的格式可以通过请求参数 output 指定，默认为 JSON 形式。
东山中学_location = response.json()['geocodes'][0]['location']      ### geocodes（地理编码信息列表） 查询字典的方式
东山中学_location                                                   ### location：坐标点(经度、维度)
---------------------------------------------------------------------
### 逆地理编码
def regeo(key,location,extensions="base"):          ### 形参（实?）
    url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "extensions":extensions,       ### 在字典中，这些对象取参数（上面提及的）
    }
    r = requests.get(url,params)
    results = r.json()
    return results                    ### 注意这里返回值用的是return
### 调用函数 显示实参
url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
params = {
    "key":key,
    "location":东山中学_location,
    "extensions":"all"
}
r = requests.get(url,params)
r                                         ### r其实就是response
### 建一个空列表_roads_,并从字典中取值后添加入列表
_roads_= []
for item in r.json()['regeocode']['roads']:         ### 上一栏中代码运行返回的结果进行取值得到的列表，['regeocode']['roads']是字典取值
#     print(item['name'])
    _roads_.append(item['name'])                    ### roads道路信息列表中的name道路信息
_roads_
-----------------------------------------------------------------------------
### 路径规划
#### 数据准备：
origin = "广东省梅州市梅县东山中学"   ### 数据为结构化地址
destination = "广东省梅州市梅雁东山学校"      ### +1


# 获取地理编码的函数:
def geocode(key, address, city=None, batch=None) -> str:
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": key,
        "address": address,
        "city": city,
        "batch": batch
    }
    response = requests.get(url, params=params)
    results = response.json()['geocodes'][0]['location']  ### 字典取值的方法取出地理编码
    return results
起始点_东山中学 = geocode(key,origin)      ### 调用函数，取实参
起始点_东山中学
目的地_东山学校 = geocode(key,destination)
目的地_东山学校
# 步行地理规划的代码：
url = "https://restapi.amap.com/v3/direction/walking?parameters"
params = {
    "key":key,
    "origin":起始点_东山中学,
    "destination":目的地_东山学校
}
r = requests.get(url,params)
r
r.json()['route']['paths']     ### paths是步行方案的意思
[{'distance': '10',
  'duration': '8',
  'steps': [{'instruction': '向南步行10米到达目的地',
    'orientation': '南',
    'road': [],
    'distance': '10',
    'duration': '8',
    'polyline': '116.135729,24.305534;116.13572,24.305503;116.135707,24.305443',
    'action': [],
    'assistant_action': '到达目的地',
    'walk_type': '0'}]}]
------------------------------------------------------------------------------------
### 公交路径规划
def bus(key,origin,destination,city,cityd,strategy):
    bus_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key":key,
        "origin":origin,
        "destination":destination,
        "city":city,
        "cityd":cityd,
        "strategy":strategy
    }
    r = requests.get(bus_url,params)
    results = r.json()
    return results
origin="113.679287,23.632575"
destination="113.495460,23.451063"
city="广州"
cityd="广州"
strategy="0"
bus(key,origin,destination,city,cityd,strategy)
-----------------------------------------------------------------------
### 行政区域查询
def municipality(key,keywords=None,subdistrict='2',page='1',offset='20',extensions='base',filter='',callback=None,output='json'):
    xzq_url = 'https://restapi.amap.com/v3/config/district?parameters'
    params = {
        'key':key,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'extensions':extensions,
        'page':page,
        'offset':offset,
        'filter':filter,
        'callback':callback,
        'output':output
    }
    r = requests.get(xzq_url,params)        ### 大姐！注意这里的空格格式呀！
    results = r.json()
    return results
keywords='广东'
subdistrict='2'
municipality(key,keywords,subdistrict='2',page='1',offset='20',extensions='base',filter='',callback=None,output='json')
### 最后一行这个格式调用也不能少了！
---------------------------------------------------------------------------------
### 搜索POI
#### 关键字搜索
def Keywords(key,keywords,types,city):
    关键字_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "types":types,
        "city":city
    }
    r = requests.get(关键字_url,params)
    results = r.json()
    return results
keywords="中山大学"
types="高等院校"
city="广州"
Keywords(key,keywords,types,city)
#### 周边搜索
### 由地理编码代码获取了location
东山中学_location='116.135586,24.305583'
def around(key,location,types,radius):
    a_url="https://restapi.amap.com/v3/place/around?parameters"
    params = {
    "key":key,
    "location":location,
    "types":types,
    "radius":radius
    }
    r = requests.get(a_url,params)
    results = r.json()
    return results
location="116.135586,24.305583"
types="超市"
radius="5000"
around(key,location,types,radius)
### 多边形搜索
# 获取地理编码的函数:
address1 = "广东省广州市中山大学南方学院"
address2 = "广东省广州市广州大学华软软件学院"
address3 = "广东省广州市华南农业大学珠江学院"


def geocode(key, address, city=None, batch=None) -> str:
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": key,
        "address": address
    }
    response = requests.get(url, params=params)
    results = response.json()['geocodes'][0]['location']
    return results
### 中大南方地理编码
中大南方_location = geocode(key,address1)
中大南方_location
### 广州大学华软软件学院地理编码
华软学院_location = geocode(key,address2)
华软学院_location
### 华南农业大学珠江学院
华农珠江_location = geocode(key,address3)
华农珠江_location
def around(key,polygon,keywords,types):
    _url="https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
    "key":key,
    "polygon":polygon,
    "types":types,
    "keywords":keywords
    }
    r = requests.get(_url,params)
    results = r.json()
    return results
polygon="113.679287,23.632575|113.495460,23.451063|113.580858,23.512186"
keywords="7-ELEVEN"
types="060201"
around(key,polygon,keywords,types)
-------------------------------------------------------------------------
### IP定位
def IP(key,ip):
    ip_url="https://restapi.amap.com/v3/ip?parameters"
    params={
        "key":key,
        "ip":ip
    }
    r=requests.get(ip_url,params)
    results=r.json()
    return results
ip="175.191.182.38"
IP(key,ip)
-------------------------------------------------------------
### 静态地图
def picture(key,location,zoom,size,traffic):
    p_url="https://restapi.amap.com/v3/staticmap?parameters"
    params={
        "key":key,
        "location":location,
        "zoom":zoom,
        "size":size,
        "traffic":traffic
    }
    r = requests.get(p_url,params)
    data = r.url                              ### 重点！！！r.url哦!!!
    return data
location="113.679287,23.632575"
zoom="17"
size="500*500"
traffic="1"
picture(key,location,zoom,size,traffic)
-------------------------------------------------------------------------------------
### 坐标转换
def convert(key,locations,coordsys):
    c_url="https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params={
        "key":key,
        "locations":locations,
        "coordsys":coordsys
    }
    r=requests.get(c_url,params)
    results=r.json()
    return results
locations="113.679287,23.632575|113.495460,23.451063"
coordsys="gps"
convert(key,location,coordsys)
--------------------------------------------------------------------------------
### 天气查询
def weather(key,city,extensions):
    w_url="https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params={
        "key":key,
        "city":city,
        "extensions":extensions
    }
    r=requests.get(w_url,params)
    results=r.json()
    return results
city="440117"
extensions="all"
weather(key,city,extensions)
----------------------------------------------------------------------
### 输入提示
def inputtips(key,city,keywords,types):
    w_url="https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params={
        "key":key,
        "city":city,
        "keywords":keywords,
        "types":types
    }
    r=requests.get(w_url,params)
    results=r.json()
    return results
city="广州"
keywords="咖啡厅"
types="0505"
inputtips(key,city,keywords,types)
